<?php
/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
 * 
 * =================================================================
 *                 MAGENTO EDITION USAGE NOTICE
 * =================================================================
 * This package designed for Magento COMMUNITY edition
 * aheadWorks does not guarantee correct work of this extension
 * on any other Magento edition except Magento COMMUNITY edition.
 * aheadWorks does not provide extension support in case of
 * incorrect edition usage.
 * =================================================================
 *
 * @category   AW
 * @package    AW_AdvancedReviews
 * @copyright  Copyright (c) 2010-2011 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
 */
class AW_AdvancedReviews_Block_Summary extends Mage_Core_Block_Template
{
    public function getSummaryBlock(){

        $productId = (int)$this->getRequest()->getParam('id');

        $parametrs = Mage::getModel('rating/rating')->getCollection();

        $ratings = array();
        foreach($parametrs as $parametr){
            $ratings[$parametr->getId()]['name'] = $parametr->getRatingCode();

            $votes = Mage::getModel('rating/rating_option_vote')->getCollection();
            $votes->getSelect()
                    ->from($this->_name,array('COUNT(*) as qty','value as mark'))
                    ->join(array('review'=>Mage::getSingleton('core/resource')->getTableName('review/review')),'main_table.review_id = review.review_id',array('status_id'))
                    ->join(array('review_store'=>Mage::getSingleton('core/resource')->getTableName('review/review_store')),'main_table.review_id = review_store.review_id',array('store_id'))
                    ->where('main_table.entity_pk_value = ?',$productId)
                    ->where('value > 0')
                    ->where('store_id = ?',Mage::app()->getStore()->getId())
                    ->where('rating_id = ?',$parametr->getId())
                    ->where('review.status_id = 1')
                    ->group('value')
                    ->order('value DESC')
                    ;

            $qtyVotes = 0;
            foreach($votes as $vote){
                $ratings[$parametr->getId()]['votes'][$vote->getMark()]['qty'] = $vote->getQty();
                $qtyVotes +=$vote->getQty();
            }
            $ratings[$parametr->getId()]['votes_qty'] = $qtyVotes;
            foreach($votes as $vote){
                $ratings[$parametr->getId()]['votes'][$vote->getMark()]['qty'] = $vote->getQty();
                $ratings[$parametr->getId()]['votes'][$vote->getMark()]['percent'] = round($vote->getQty()/$qtyVotes*100);
            }

        }
        return $ratings;

    }

}
